﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace scmsByAspdNet.cotro.student {
  /// <summary>
  /// getAllStudent 的摘要说明
  /// </summary>
  public class getAllStudent : IHttpHandler {

    public void ProcessRequest(HttpContext context) {
      context.Response.ContentType = "text/plain";

      //获得
      SqlConnection conn = new SqlConnection("Data Source=106.54.69.143;database=scms;uid=sa;pwd=Aspdotnet666");
      conn.Open();

      SqlCommand sql_rows = new SqlCommand("select*from sc_student", conn);
      string rows = "[";
      SqlDataReader reader = sql_rows.ExecuteReader();
      for (; reader.Read();) {
        rows += $"{{\"id\":\"{reader[0].ToString()}\",\"name\":\"{reader[1].ToString()}\",\"faculty\":\"{reader[3].ToString()}\",\"class\":\"{reader[4].ToString()}\"}},";
      }
      rows = rows.Substring(0, rows.Length - 1);
      rows += "]";
      reader.Close();
      conn.Close();

      //返回数据
      string ret = "{" +
        "\"rows\":" + rows + "" +
        "}";
      context.Response.StatusCode = 200;
      context.Response.Write(ret);
    }

    public bool IsReusable {
      get {
        return false;
      }
    }
  }
}